import java.util.ArrayDeque;
import java.util.HashMap;

/**
 * @author luoqingbo
 * @create 2024/5/9 14:23
 */
public class Leetcode20 {
    public static void main(String[] args) {
        String s = "()";
        System.out.println(isValid(s));
    }

    public static boolean isValid(String s) {
        HashMap<Character, Character> map = new HashMap<>();
        map.put(')', '(');
        map.put('}', '{');
        map.put(']', '[');
        char[] a = s.toCharArray();
        ArrayDeque<Character> stack = new ArrayDeque<>();
        for (int i = 0; i < a.length; i++) {
            if (a[i] == '(' || a[i] == '{' || a[i] == '[') {
                stack.push(a[i]);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                if (stack.peek() == map.get(a[i])) {
                    stack.pop();
                } else {
                    stack.push(a[i]);
                }
            }
        }
        return stack.isEmpty();
    }
}
